package com.deliveryservicesystem.app.factory;

import com.deliveryservicesystem.model.*;
import java.util.Date;

/**
 * File Name: AppFactory.java
 * Version:
 * Description:
 * Package: com.deliveryservicesystem.app.factory
 * Author: Lance
 * Date: Jun 24, 2012
 * Editor:
 */
public class AppFactory
{

    public static Customer createCustomer(String address, String contactNo, String customerNo, String firstName, String lastName)
    {
        String fName = firstName.substring(0, 1).toUpperCase() + firstName.substring(1).toLowerCase();
        String lName = lastName.substring(0, 1).toUpperCase() + lastName.substring(1).toLowerCase();

        Customer c = new Customer();
        if (!customerNo.equals("New Number is Generated"))
        {
            c.setCustomerNo(Integer.parseInt(customerNo));
        }
        c.setFirstName(fName.trim());
        c.setLastName(lName.trim());
        c.setAddress(address.trim());
        c.setContactNo(contactNo.trim());
        return c;
    }

    public static void createUpdatedCustomer(Customer updatedCustomer, Customer customerToUpdate)
    {
        customerToUpdate.setFirstName(updatedCustomer.getFirstName().substring(0, 1).toUpperCase() + updatedCustomer.getFirstName().substring(1).toLowerCase());
        customerToUpdate.setLastName(updatedCustomer.getLastName().substring(0, 1).toUpperCase() + updatedCustomer.getLastName().substring(1).toLowerCase());
        customerToUpdate.setAddress(updatedCustomer.getAddress().trim());
        customerToUpdate.setContactNo(updatedCustomer.getContactNo().trim());
    }

    public static Billing createBilling(String billingAddress, String totalAmount, String billingNo)
    {
        Billing b = new Billing();
        if (!billingNo.equals("New Number is Generated"))
        {
            b.setBillingNo(Integer.parseInt(billingNo));
        }
        b.setBillingAddress(billingAddress.trim());
        b.setBillingAmount(Double.parseDouble(totalAmount));
        return b;
    }

    public static void createUpdatedBilling(Billing updatedBilling, Billing billingToUpdate)
    {
        billingToUpdate.setBillingAddress(updatedBilling.getBillingAddress().trim());
        billingToUpdate.setBillingAmount(updatedBilling.getBillingAmount());
    }

    public static Delivery createDelivery(Date deliveryDate, Character isDelivered, String deliveryNo, Driver driverNo)
    {
        Delivery delivery = new Delivery();
        if (!deliveryNo.equals("New Number is Generated"))
        {
            delivery.setDeliveryNo(Integer.parseInt(deliveryNo));
        }
        delivery.setDeliveryDate(deliveryDate);
        delivery.setIsDelivered(isDelivered);
        delivery.setDriverNo(driverNo);
        return delivery;
    }

    public static void createUpdatedDelivery(Delivery updatedDelivery, Delivery deliveryToUpdate)
    {
        deliveryToUpdate.setDeliveryDate(updatedDelivery.getDeliveryDate());
        deliveryToUpdate.setDeliveryNo(updatedDelivery.getDeliveryNo());
        deliveryToUpdate.setDriverNo(updatedDelivery.getDriverNo());
        deliveryToUpdate.setIsDelivered(updatedDelivery.getIsDelivered());
    }

    public static Driver createDriver(String address, String contactNo, String customerNo, String firstName, String lastName, String driverNo, String vehicleRegistration)
    {
        String fName = firstName.substring(0, 1).toUpperCase() + firstName.substring(1).toLowerCase();
        String lName = lastName.substring(0, 1).toUpperCase() + lastName.substring(1).toLowerCase();

        Driver driver = new Driver();
        if (!driverNo.equals("New Number is Generated"))
        {
            driver.setDriverNo(Integer.parseInt(driverNo));
        }
        driver.setAddress(address);
        driver.setContactNo(contactNo);
        driver.setFirstName(fName);
        driver.setLastName(lName);
        driver.setVehicleRegistration(vehicleRegistration);
        return driver;
    }

    public static void createUpdatedDriver(Driver updatedDriver, Driver driverToUpdate)
    {
        driverToUpdate.setDriverNo(updatedDriver.getDriverNo());
        driverToUpdate.setAddress(updatedDriver.getAddress());
        driverToUpdate.setContactNo(updatedDriver.getContactNo());
        driverToUpdate.setFirstName(updatedDriver.getFirstName());
        driverToUpdate.setLastName(updatedDriver.getLastName());
        driverToUpdate.setVehicleRegistration(updatedDriver.getVehicleRegistration());
    }

    public static Product createProduct(String productNo, String productPrice, String productDesc, Supplier supplier, Subscription subscription)
    {
        Product product = new Product();
        if (!productNo.equals("New Number is Generated"))
        {
            product.setProductNo(Integer.parseInt(productNo));
        }
        product.setProductDescription(productDesc);
        product.setProductPrice(Double.parseDouble(productPrice));
        product.setSubscriptionNo(subscription);
        product.setSupplierNo(supplier);
        return product;
    }

    public static void createUpdatedProduct(Product updatedProduct, Product productToUpdate)
    {
        productToUpdate.setProductDescription(updatedProduct.getProductDescription());
        productToUpdate.setProductNo(updatedProduct.getProductNo());
        productToUpdate.setProductPrice(updatedProduct.getProductPrice());
        productToUpdate.setSubscriptionNo(updatedProduct.getSubscriptionNo());
        productToUpdate.setSupplierNo(updatedProduct.getSupplierNo());
    }

    public static Route createRoute(String routeNo, String mainRoute, Delivery delivery)
    {
        Route route = new Route();
        if (!routeNo.equals("New Number is Generated"))
        {
            route.setRouteNo(Integer.parseInt(routeNo));
        }
        route.setDeliveryNo(delivery);
        route.setMainRoute(mainRoute);
        return route;
    }

    public static void createUpdatedProduct(Route updatedRoute, Route routeToUpdate)
    {
        routeToUpdate.setDeliveryNo(updatedRoute.getDeliveryNo());
        routeToUpdate.setMainRoute(updatedRoute.getMainRoute());
        routeToUpdate.setRouteNo(updatedRoute.getRouteNo());
    }

    public static Subscription createSubscription(String subscriptionNo, Date subscriptionDate, String baseAmount, String totalAmount, Billing billing, Customer customer, Delivery delivery)
    {
        Subscription subscription = new Subscription();
        if (!subscriptionNo.equals("New Number is Generated"))
        {
            subscription.setSubscriptionNo(Integer.parseInt(subscriptionNo));
        }
        subscription.setBaseAmount(Double.parseDouble(baseAmount));
        subscription.setBillingNo(billing);
        subscription.setCustomerNo(customer);
        subscription.setDeliveryNo(delivery);
        subscription.setSubscriptionDate(subscriptionDate);
        subscription.setTotalAmount(Double.parseDouble(totalAmount));
        return subscription;
    }

    public static void createUpdatedSubscription(Subscription updatedSubscription, Subscription subscripionToUpdate)
    {
        subscripionToUpdate.setBaseAmount(updatedSubscription.getBaseAmount());
        subscripionToUpdate.setBillingNo(updatedSubscription.getBillingNo());
        subscripionToUpdate.setCustomerNo(updatedSubscription.getCustomerNo());
        subscripionToUpdate.setDeliveryNo(updatedSubscription.getDeliveryNo());
        subscripionToUpdate.setSubscriptionDate(updatedSubscription.getSubscriptionDate());
        subscripionToUpdate.setSubscriptionNo(updatedSubscription.getSubscriptionNo());
        subscripionToUpdate.setTotalAmount(updatedSubscription.getTotalAmount());
    }

    public static Supplier createSupplier(String supplierNo, String supplierName, String supplierContact, String supplierAddress)
    {
        String suppName = supplierName.substring(0, 1).toUpperCase() + supplierName.substring(1).toLowerCase();
        Supplier supplier = new Supplier();
        if (!supplierNo.equals("New Number is Generated"))
        {
            supplier.setSupplierNo(Integer.parseInt(supplierNo));
        }
        supplier.setAddress(supplierAddress);
        supplier.setContactNo(supplierContact);
        supplier.setSupplierName(suppName);
        return supplier;
    }

    public static void createUpdatedCustomer(Supplier updatedSupplier, Supplier supplierToUpdate)
    {
        supplierToUpdate.setAddress(updatedSupplier.getAddress());
        supplierToUpdate.setContactNo(updatedSupplier.getContactNo());
        supplierToUpdate.setSupplierName(updatedSupplier.getSupplierName());
        supplierToUpdate.setSupplierNo(updatedSupplier.getSupplierNo());
    }
}